

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bean.Employee;
import bean.Flight;

import service.AirlineReservationServiceProxy;
import constants.AirlineReservationServiceConstants;

/**
 * Servlet implementation class SearchEmployee
 */
public class SearchEmployeeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SearchEmployeeServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		PrintWriter out=response.getWriter();
		AirlineReservationServiceProxy proxy = new AirlineReservationServiceProxy(
				AirlineReservationServiceConstants.SERVICE_URL);
		try{
		String fname = request.getParameter("fname");
		String lname = request.getParameter("lname");
		String role = request.getParameter("role");
		String workdesc = request.getParameter("desc");
		String position = request.getParameter("pos");
		boolean flag = false;
		//int numTravelers = Integer.parseInt(request.getParameter("numTravelers"));
		List<String> tbl= new ArrayList<String>();
		List<String> paramName = new ArrayList<String>();
		List<String> paramValue = new ArrayList<String>();
		if(!(fname.equals(""))){
			tbl.add("person");
			paramName.add("first_name");
			paramValue.add(fname);
			flag = true;
		}
		if(!(lname.equals(""))){
			tbl.add("person");
			paramName.add("last_name");
			paramValue.add(lname);
			flag = true;
		}
		if(!(role.equals(""))){
			tbl.add("employee");
			paramName.add("role");
			paramValue.add(role);
			flag = true;
		}	
		if(!(workdesc.equals(""))){
			tbl.add("employee");
			paramName.add("work_description");
			paramValue.add(workdesc);
			flag = true;
		}	
		if(!(position.equals(""))){
			tbl.add("employee");
			paramName.add("position");
			paramValue.add(position);
			flag = true;
		}	
		
	/*	for(int i=0; i<paramName.size();i++){
			out.println(i);
			out.println(paramName.get(i));
		}
		for(int i=0; i<paramValue.size();i++){
			
			out.println("param value");
			out.println(i);
			out.println(paramValue.get(i));
		}	*/
		
		
		if(flag == false){
			request.setAttribute("error", "Please select at least one attribute");
			RequestDispatcher requestDispatcher=request.getRequestDispatcher("employeeSearch.jsp");
			requestDispatcher.forward(request,response);
			
		}
		
		String[] arrTbl = new String[tbl.size()];
		String[] arrParamName = new String[paramName.size()];
		String[] arrParamValue = new String[paramValue.size()];
		
		Employee[] employees = proxy.searchEmployeebyAttr(tbl.toArray(arrTbl), paramName.toArray(arrParamName),paramValue.toArray(arrParamValue) );
	/*	for(Employee emp:employees){
			out.println("Inside main");
			out.println(emp.getFirstName());
			out.println(emp.getLastName());
			out.println(emp.getUserName());
			out.println(emp.getAddressLine1());
			out.println(emp.getAddressLine2());
			out.println(emp.getCity());
			out.println(emp.getState());
			out.println(emp.getPosition());
			out.println(emp.getRole());
			out.println(emp.getHireDate());
			out.println(emp.getWorkDescription());
		} */
		
		/* trial for testing
		Employee e=new Employee();
		e.setFirstName("Unnati");
		e.setLastName("Khaskia");
		e.setAddressLine1("101 E. san fernando");
		e.setAddressLine2("apt 326");
		e.setCity("surat");
		e.setState("CA");
		e.setId(1);
		e.setPosition("parttime");
		e.setRole("intern");
		e.setWorkDescription("developer");
		List<Employee> employees = new ArrayList<Employee>();
		employees.add(e);   
		out.println("Added to list"); */
		try{request.getSession().setAttribute("empLst",employees);
	    RequestDispatcher requestDispatcher=request.getRequestDispatcher("employeeSearch.jsp");
	    requestDispatcher.forward(request,response);
		
	    }catch(Exception ex){System.out.println("error...."+ ex.toString());}
		
		}catch(Exception ex){out.println("@@@"+ex.toString());}
	}

}
